package com.putao.tinytime.fragments;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.putao.tinytime.R;
import com.putao.tinytime.activities.EditActivity;
import com.putao.tinytime.base.BaseFragment;
import com.putao.tinytime.bean.CollageConfigInfo;
import com.putao.tinytime.util.BitmapHelper;
import com.putao.tinytime.util.CollageInfoManager;
import com.putao.tinytime.util.DisplayHelper;
import com.putao.tinytime.util.Loger;
import com.putao.tinytime.util.StringHelper;
import com.putao.tinytime.view.SceneSynthesisView;

import java.util.ArrayList;

/**
 * Created by jidongdong on 15/3/20.
 */
public class SenceFragment extends BaseFragment {
    private SceneSynthesisView sence_view;
    private LinearLayout parent_layout;
    private float sample_scale = 1.0f;
    private CollageConfigInfo.SenceItemInfo item;
    private CollageInfoManager resourceManager;
    private int photo_index = -1;

    @Override
    public int doGetContentViewId() {
        return R.layout.fragment_sence_layout;
    }


    @Override
    public void doInitSubViews(View view) {
        sence_view = (SceneSynthesisView) view.findViewById(R.id.sence_view);
        parent_layout = (LinearLayout) view.findViewById(R.id.parent_layout);
        sence_view.setOnPhotoAreaSelectedListener(new SceneSynthesisView.OnPhotoAreaSelectedListener() {
            @Override
            public void selectedChanged(int index) {
                ((EditActivity) mActivity).disableViewPagerScroll((index > -1) ? true : false);
            }
        });
    }

    @Override
    public void doInitDataes() {
        super.doInitDataes();
        resourceManager = CollageInfoManager.getInstance();
        sample_scale = DisplayHelper.getDensity() / 2;
        Bundle bundle = getArguments();
        item = (CollageConfigInfo.SenceItemInfo) bundle.getSerializable("item");
        if (!StringHelper.isEmpty(item.background)) {
            parent_layout.setBackgroundDrawable(new BitmapDrawable(resourceManager.getSampleResourceImage(item.background, sample_scale)));
        }
        initSenceView();
    }

    public void setPhoto(String photo_path) {
        if (photo_index > -1) {
            sence_view.changePhoto(BitmapHelper.getInstance().loadBitmap(photo_path, DisplayHelper.getScreenWidth(), DisplayHelper.getScreenHeight()), photo_index);
        }
    }


    void initSenceView() {
        if (item == null) {
            Loger.d("sence item info is null!!!");
            return;
        }
        sence_view.setImageList(getPhotoSetFromCollageItemInfo());
        sence_view.setOnPhotoAreaClickListener(new SceneSynthesisView.OnPhotoAreaClickListener() {
            @Override
            public void clicked(int index) {
                photo_index = index;
                ((EditActivity) mActivity).startSelectPhoto();
            }
        });
    }

    private ArrayList<SceneSynthesisView.CollagePhotoSet> getPhotoSetFromCollageItemInfo() {
        ArrayList<SceneSynthesisView.CollagePhotoSet> photoSet = new ArrayList<>();
        if (photoSet != null) {
            Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.drawable.no_photo)).getBitmap();

            if (bitmap != null) {
                for (int i = 0; i < item.photoElements.size(); i++) {
                    CollageConfigInfo.PhotoItemInfo photo_info = item.photoElements.get(i);
                    Bitmap bg = resourceManager.getSampleResourceImage(photo_info.background, sample_scale);
                    Bitmap fg = resourceManager.getSampleResourceImage(photo_info.foreground, sample_scale);
                    photoSet.add(sence_view.new CollagePhotoSet(bitmap, bg, fg, photo_info.left * sample_scale, photo_info.top * sample_scale, getImagePointsByCollageImageInfo(item.photoElements.get(i).photo_area)));
                }
            }
        }
        return photoSet;
    }

    private SceneSynthesisView.Area getImagePointsByCollageImageInfo(ArrayList<CollageConfigInfo.PhotoAreaPoint> area) {
        int count = area.size();
        float[] p_x = new float[count];
        float[] p_y = new float[count];
        for (int i = 0; i < count; i++) {
            p_x[i] = area.get(i).point_x;
            p_y[i] = area.get(i).point_y;
        }
        SceneSynthesisView.Area new_area = sence_view.new Area(sence_view.getScalePloyX(p_x, sample_scale), sence_view.getScalePloyY(p_y, sample_scale));
        return new_area;
    }
}
